package com.helsinki.controller;

import com.github.pagehelper.PageInfo;
import com.helsinki.pojo.Permission;
import com.helsinki.pojo.Product;
import com.helsinki.service.PermissionService;
import com.helsinki.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("permission")
public class PermissionController {
    @Autowired
    private PermissionService permissionService;

    @RequestMapping("list")
    @ResponseBody
    public PageInfo<Permission> list(@RequestParam(defaultValue = "1") Integer pageNum,
                                     @RequestParam(defaultValue = "5") Integer pageSize) {
        return permissionService.queryAll(pageNum, pageSize);
    }

    @RequestMapping("findById")
    @ResponseBody
    public Permission findById(@RequestParam String id) {
        return permissionService.queryById(id);
    }

    @RequestMapping("insert")
    public String save(@RequestBody Permission permission) throws Exception {
        String str = "";
        try {
            permissionService.addPermission(permission);
            str = "添加成功,";
        } catch (Exception e) {
            e.printStackTrace();
            str = "添加失败！请重新添加！！！";
        }
        return "redirect:/pages/permission-list.html";
    }

    @RequestMapping("update")
    public String update(@RequestBody Permission permission) {
        permissionService.updatePermission(permission);
        return "redirect:/pages/permission-list.html";
    }

    //删除权限
    @RequestMapping("/deletePermission")
    @ResponseBody
    public Boolean delete(@RequestBody String id){
        id = id.substring(0,id.length()-1);
        return permissionService.deleteById(id);
    }


}
